import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import an49 from '@mmstudio/an000049';
import an8 from '@mmstudio/an000008';
import { Content } from 'antd/lib/layout/layout';
import { message } from 'antd';
import { tbcourseassignment, tbjob, tbjobpublish, tbteacherlist, tbtheteacherisqualifiedforthecurriculum } from '../../../../atoms/db';


const logger = anylogger('pages/api/test.api');

export type Result = {
	name: string;
	ok: true;

} | {

	ok: false;
	message: string;

};

export type Message = {
	jobcontent: string;
	jobtitle: string;
	date: string;
}
/**
 * pages/api/test.api
 */
const handler = an48<Result>();
handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { jobcontent, jobtitle } = req.body as Message;
		//**获取用户教师的教师id */
		const teacher_id = 'T001';
		//**查出发布作业老师姓名 */
		const teachername = await tbteacherlist().first('teachername').where({
			teacher_id: teacher_id
		});
		//**转换类型 */
		const Teachername = teachername.teachername;
		//**获取教师胜任课程名称   此处仅考虑教师仅胜任一门的情况*/
		const coursename = await tbtheteacherisqualifiedforthecurriculum().first('coursename').where({
			teacher_id: teacher_id
		});
		const Coursename = coursename.coursename;
		const db = an49();
		const tb = db<ITbcourseassignment>('courseassignment');
		if (!jobcontent) {
			res.status(200).json({
				ok: false,
				message: '请输入作业内容'
			});
			return;
		}
		if (!jobtitle) {
			res.status(200).json({
				ok: false,
				message: '请输入作业标题'
			});
			return;
		}
		const date = Date.now();
		const id = an8();
		logger.debug(' id', id);
		await tb.insert({
			jobcontent,
			jobtitle,
			job_id: id,
			date: date,
			teachername: Teachername,
			coursename: Coursename
		});



		res.status(200).json({
			ok: true,
			name: '发布成功',


		});
	} catch (error) {
		logger.trace(error);
		res.status(200).json({
			ok: false,
			message: '参数错误',

		});
	}
});

export const config = {} as PageConfig;

export default handler;
